package com.springboot.controller;

import com.springboot.entity.Entity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

/**
 * @author yexi
 */
@RestController
public class TestController {

    @RequestMapping(value = "/param")
    public Object param1(HttpServletRequest httpRequest) {
        System.out.println(httpRequest.getContentType());
        System.out.println(httpRequest.getMethod());
        System.out.println(httpRequest.getContextPath());
        System.out.println(httpRequest.getPathInfo());
        System.out.println(httpRequest.getRequestURL());
        httpRequest.getParameterMap().forEach((s, strings) -> System.out.println(s + "-" + Arrays.toString(strings)));
        try (ServletInputStream inputStream = httpRequest.getInputStream()) {
            byte[] bytes = new byte[8192];
            int i = inputStream.readLine(bytes, 0, 8192);
            System.out.println(new String(bytes, 0, i));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "这是返回去的信息";
    }

    @RequestMapping(value = "/param2")
    public Object param2(@RequestBody Entity entity, @RequestParam String other) {
        System.out.println(entity);
        System.out.println(other);
        return "这是返回去的信息";
    }

    @GetMapping("http/login")
    public String hehe(HttpServletRequest httpRequest) throws IOException {
        System.out.println(httpRequest.getContentType());
        System.out.println(httpRequest.getMethod());
        Map<String, String[]> parameterMap = httpRequest.getParameterMap();
        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
            System.out.println(stringEntry.getKey());
            System.out.println(Arrays.toString(stringEntry.getValue()));
        }
        BufferedReader reader = httpRequest.getReader();
        System.out.println(reader.readLine());


        System.out.println("进来了");
        return "呵呵";
    }

    @RequestMapping(value = "/test", method = {RequestMethod.POST})
    public String startGpsGreenRoad(HttpServletRequest httpRequest, LinkedHashMap<String, Object> parase) throws IOException, ServletException {
        System.out.println(parase);
        System.out.println(httpRequest.getContentType());
        System.out.println(httpRequest.getMethod());
        Map<String, String[]> parameterMap = httpRequest.getParameterMap();
        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
            System.out.println(stringEntry.getKey());
            System.out.println(Arrays.toString(stringEntry.getValue()));
        }
//        BufferedReader reader = httpRequest.getReader();
//        System.out.println(reader.readLine());
        System.out.println(httpRequest.getContextPath());
        System.out.println(httpRequest.getPathInfo());
        System.out.println(httpRequest.getRequestURL());
//        System.out.println(parase);
        Collection<Part> parts = httpRequest.getParts();
        System.out.println(parts);
        return "搭档";
    }

    @RequestMapping(value = "/test2", method = {RequestMethod.POST})
    @ResponseBody
    public void asdasd(@RequestParam(value = "params") List<String> params) {
        List<String> params1 = params;
        System.out.println(params1);
    }


    /**
     * params="name"：要求请求中必须携带指定名称的参数
     * params="name=dafei"：要求请求中必须携带指定名称的参数:name,并且name=dafei,但一般不采用这种方式，所以没什么用
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET, params = "name=dafei")
    @ResponseBody
    public Object test1() {
        return "ok";
    }

    /**
     * headers:请求头，要求过来时请求头必须带上一些什么
     * Content-Type：代表客户端/服务器 携带的数据的类型 用处：有些接收的客户端不一样，使用的格式不一定一样，所以这边后端处理需要做区分
     * 最重要的一点是：带上了它是属于路径区分的一份子的，会认为是不一样的请求路径
     */
    /*@RequestMapping(value = "/test2", method = RequestMethod.GET, headers = "Content-Type=application/xml")*/
    //可以优化成（其他的可以自己进去底层看）:
    @RequestMapping(value = "/test2", method = RequestMethod.GET, consumes = "application/xml")
    @ResponseBody
    public Object test2() {
        return "ok-xml";
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET, headers = "Content-Type=application/json")
    @ResponseBody
    public Object test3() {
        return "ok-json";
    }
}

